package org.jeecg.modules.iot.model;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UniqueSerialNumberGenerator {

    private static int sequence = 0;

    public static synchronized String generateSerialNumber(String vin) {
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();

        // 格式化时间戳为指定格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timestampStr = dateFormat.format(new Date(timestamp));

        // 拼接VIN和时间戳生成流水号
        String serialNumber = vin + timestampStr;

        // 可选：添加序列号以增加唯一性
        serialNumber += getNextSequence();

        return serialNumber;
    }

    public static synchronized String getNextSequence() {
        // 自增序列号
        sequence = ((sequence + 1) & 0xffff);
        String sequenceStr = String.format("%04d", sequence);
        return sequenceStr;
    }

    public static void main(String[] args) {
        String vin = "ABC123XYZ456";
        //String serialNumber = generateSerialNumber(vin);
        for (int i = 0; i <65577 ; i++) {
            System.out.println("Generated Serial Number: " + getNextSequence());
        }



    }

}
